//
// Created by THE13 on 2024/2/6.
//

#include "mvvm.h"
/* User Defines   ---------------------------------------------------------*/
#define FIRST_PAGE_POINTER sys_info.now->last->last->next //第一页的指针
#define NOW_PAGE_POINTER   sys_info.now->last
/* Exported functions --------------------------------------------------------*/
void animation_init(void)
{
    LIST_Animation_init();
}
/* Private functions --------------------------------------------------------*/
static uint8_t onViewWillDisappear_View(menu_t *page)
{
    switch(page->pagetype)
    {
        case LIST: list_view(page); break;
        case DATA:/* chartDataViewModel_Animation(Chart_Data);pageGradualDisappearanceAnimation(page);*/ break;
        case ICON: /*pageIconViewModel_Animation(page);*/break;
        case TEXT: /*textViewModel_Animation();pageGradualDisappearanceAnimation(page); */break;
        default: break;
    }
    return 0;
}

static uint8_t onViewWillAppear_View(menu_t *page)
{
    //1.正常显示 begin
    switch(page->pagetype)
    {
        case LIST: list_view(page); break;
        case DATA:/* chartDataViewModel_Animation(Chart_Data); */break;
        case ICON:/* pageIconViewModel_Animation(page); */break;
        case TEXT: /*textViewModel_Animation(); */break;
        default: break;
    }
    if(sys_info.now->itemtype == ITEM_CALL_PAGE)
    {
        if(sys_info.now->param2 ==1)//按键按下
        {
            //开始注销页面动画
            page->state = onViewWillDisappear;

            return 1;
        }
    }
    return 0;
}

static uint8_t onViewDidAppear_View(menu_t *page)
{
    //1.正常显示 begin
    switch(page->pagetype)
    {
        case LIST: list_view(page);break;
        case DATA: /*chartDataViewModel_Animation(Chart_Data);*/break;
        case ICON:/* pageIconViewModel_Animation(page); */break;
        case TEXT: /*textViewModel_Animation();*/ break;
        default: break;
    }
    //2.如果按下就开始切换页面 begin
    if(sys_info.now->itemtype == ITEM_CALL_PAGE)
    {
        if(sys_info.now->param2 ==1)//按键按下
        {
            //开始注销页面动画
            page->state = onViewWillDisappear;

            return 1;
        }
    }
    return 0;
}


//不同类型页面动画切换，页面消失的优先级高于页面显示
/**
  * @brief  pageViewModel
  * @retval page status
  * @note   不同类型页面动画切换，页面消失的优先级高于页面显示
  */
uint8_t ViewController(void)
{
    menu_t* Page;

    static uint16_t page_stateCount = 0;

    /*onViewWillDisappear*/
    Page = FIRST_PAGE_POINTER;
    for(;Page!= nullptr;Page = Page->same_next)
    {
        if(Page ->state == onViewWillDisappear)
        {
            page_stateCount++;
            onViewWillDisappear_View(Page);
            //计算20帧就进入下一个状态
            if(page_stateCount>=5)
            {
                Page->state = onViewDidDisappear;
                return 1;
            }
        }
    }
    /*onViewDidDisappear*/
    Page = FIRST_PAGE_POINTER;//获取第一页的指针
    for(;Page!=nullptr;Page = Page->same_next)
    {
        if(Page ->state == onViewDidDisappear)
        {
            //页面结束前最后命令 begin------*/
            animation_init();
            Page->state = onViewLoard;
            //页面结束前最后命令 end------*/


            //切换页面 begin------*/
            //1.获取目标页面的的指针
            uint8_t PAGE_ID = sys_info.now->param1;
            Page  = node->next;//获取第一页
            for(;PAGE_ID!=0;PAGE_ID--)
            {
                Page = Page->same_next;
            }
            //2.指向目标页面的第一个选项
            sys_info.now->param2 = 0;
            Page->state = onViewWillAppear;//设置目标页面的动画状态
            sys_info.now = Page->next;//指向目标页面的第一个选项
            //切换页面  end------*/
        }
    }
    /*onViewLoard*/
    Page = NOW_PAGE_POINTER;//获取当前页面指针
    if(Page->state == onViewLoard)
    {
        Page->state = onViewDidLoard;
    }
    /*onViewDidLoard*/
    Page = NOW_PAGE_POINTER;//获取当前页面指针
    if(Page->state == onViewDidLoard)
    {
        Page->state = onViewWillAppear;
    }
    /*onViewWillAppear、onViewDidAppear*/
    Page = NOW_PAGE_POINTER;//获取当前页面指针
    if((Page->state == onViewWillAppear)||(Page->state == onViewDidAppear))
    {
        onViewWillAppear_View(Page);
        page_stateCount = 0;
    }
    return 0;
}